/*
 *Copyright (c) 2012
 *author skyrain
 */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

/*
 * COMMAND_ID
 */
#define SGIP_BIND (0x1)
#define SGIP_BIND_RESP (0x80000001)
#define SGIP_UNBIND  (0x2)
#define SGIP_UNBIND_RESP  (0x80000002)
#define SGIP_SUBMIT  (0x3)
#define SGIP_SUBMIT_RESP  (0x80000003)
#define SGIP_DELIVER  (0x4)
#define SGIP_DELIVER_RESP  (0x80000004)
#define SGIP_REPORT  (0x5)
#define SGIP_REPORT_RESP  (0x80000005)
#define SGIP_USERRPT  (0x11)
#define SGIP_USERRPT_RESP  (0x80000011)
#define SGIP_TRACE  (0x1000)
#define SGIP_TRACE_RESP  (0x80001000)

#define LEN_SGIP_HEADER  (20)
#define LEN_BIND  (41)
#define LEN_BIND_RESP  (9)
#define LEN_SUBMIT  (123)
#define LEN_SUBMIT_RESP  (9)
#define LEN_DELIVER  (57)
#define LEN_DELIVER_RESP  (9)
#define LEN_REPORT  (44)
#define LEN_REPORT_RESP  (9)
#define LEN_USERRPT  (51)
#define LEN_USERRPT_RESP  (9)

#pragma pack(1) // 按照1字节方式进行对齐
typedef struct
{
u_int32_t msg_len;
u_int32_t command_id;
u_int32_t node_id;
u_int32_t time;
u_int32_t seq;
} msg_head;

typedef struct
{
u_int8_t login_type;
char login_name[16];
char login_password[16];
char reserve[8];
} msg_bind;

typedef struct
{
u_int8_t result;
char reserve[8];
} msg_bind_resp;

typedef struct
{
char sp_number[21];
char charge_number[21];
char user_count;
char user_num[21];
char corp_id[5];
char service[10];
char fee_type;
char given_Value;
char agent_flag;
char morelatetomtflag;
char priorty;
char expire_time[16];
char schedule_time[16];
char report_flag;
char tp_pid;
char tp_udhi;
char message_coding;
char message_type;
u_int16_t message_length;
char *message_content;
char reserve[8];
} msg_submit;

typedef struct
{
char result;
char reserve[8];
} msg_submit_resp;

typedef struct
{
char user_number[21];
char sp_number[21];
char tp_pid;
char tp_udhi;
char message_coding;
u_int32_t message_length;
char *message_content;
char reserve[8];
} msg_deliver;

typedef struct
{
char result;
char reserve[8];
} msg_deliver_resp;

typedef struct
{
char sub_seq_num[12];
char report_type;
char user_number[21];
char state;
char error_code;
char reserve[8];
} msg_report;

typedef struct
{
char result;
char reserve[8];
} msg_report_resp;

typedef struct
{
char sp_number[21];
char user_number[21];
char user_condition;
char reserve[8];
} msg_userrpt;

typedef struct
{
char result;
char reserve[8];
} msg_userrpt_resp;

#pragma pack() // 取消1字节对齐方式
void err_dump(const char *, ...);
void err_msg(const char *, ...);
void err_quit(const char *, ...);
void err_ret(const char *, ...);
void err_sys(const char *, ...);
int Sendmsg_tcp(const int, struct iovec*, int, int);
u_int32_t time2int(void);
